﻿#include "mypushbuttonfourarithmetic_p.h"

MyPushButtonFourArithmeticPrivate::MyPushButtonFourArithmeticPrivate()
    : imageLayerIndex(1),
      leftOperandDeviceIndex(1),
      rightOperandDeviceIndex(1),
      leftOperandObjectType(My::D),
      rightOperandObjectType(My::D),
      leftOperandObjectIndex(0),
      rightOperandObjectIndex(0),
      arithmeticType(My::Addition),
      leftOperandNumberType(My::Constant),
      rightOperandNumberType(My::Constant),
      leftOperandBaseType(My::Dec),
      rightOperandBaseType(My::Dec),
      leftOperand(0),
      rightOperand(0),
      resultDeviceIndex(0),
      resultObjectType(My::D),
      resultObjectIndex(0),
      displayDefault(true),
      LeftmousePressed(false),
      mousePressPoint(0, 0)
{

}

MyPushButtonFourArithmeticPrivate::~MyPushButtonFourArithmeticPrivate()
{

}

MyPushButtonFourArithmetic::MyPushButtonFourArithmetic(QWidget *parent)
    : QPushButton(parent),
      d(new MyPushButtonFourArithmeticPrivate)
{

}

MyPushButtonFourArithmetic::~MyPushButtonFourArithmetic()
{
    delete d;
}

/*!
    \property MyPushButtonFourArithmetic::imageLayerIndex
    \brief the index of the imageLayer

    The imageLayer is the layer of the widget.

    The default imageLayerIndex is 1.

    \sa indent
*/
int MyPushButtonFourArithmetic::getImageLayerIndex() const
{
    return d->imageLayerIndex;
}

void MyPushButtonFourArithmetic::setImageLayerIndex(int imageLayerInd)
{
    if(d->imageLayerIndex == imageLayerInd)
        return;
    d->imageLayerIndex = imageLayerInd;
}

/*!
    \property MyPushButtonFourArithmetic::leftOperandDeviceIndex
    \brief the index of the leftOperandDevice

    The Device is the paired PLC.

    The default leftOperandDeviceIndex is 1.

    \sa indent
*/
int MyPushButtonFourArithmetic::getLeftOperandDeviceIndex() const
{
    return d->leftOperandDeviceIndex;
}

void MyPushButtonFourArithmetic::setLeftOperandDeviceIndex(int deviceInd)
{
    if(d->leftOperandDeviceIndex == deviceInd)
        return;
    d->leftOperandDeviceIndex = deviceInd;
}

/*!
    \property MyPushButtonFourArithmetic::rightOperandDeviceIndex
    \brief the index of the rightOperandDevice

    The Device is the paired PLC.

    The default rightOperandDeviceIndex is 1.

    \sa indent
*/
int MyPushButtonFourArithmetic::getRightOperandDeviceIndex() const
{
    return d->rightOperandDeviceIndex;
}

void MyPushButtonFourArithmetic::setRightOperandDeviceIndex(int deviceInd)
{
    if(d->rightOperandDeviceIndex == deviceInd)
        return;
    d->rightOperandDeviceIndex = deviceInd;
}

/*!
    \property MyPushButtonFourArithmetic::leftOperandObjectType
    \brief the type of the leftOperandObject

    The object is coil or register of PLC.

    The default leftOperandObjectType is D.

    \sa indent
*/
My::ObjectType MyPushButtonFourArithmetic::getLeftOperandObjectType() const
{
    return d->leftOperandObjectType;
}

void MyPushButtonFourArithmetic::setLeftOperandObjectType(My::ObjectType type)
{
    if(d->leftOperandObjectType == type)
        return;
    d->leftOperandObjectType = type;
}

/*!
    \property MyPushButtonFourArithmetic::rightOperandObjectType
    \brief the type of the rightOperandObject

    The object is coil or register of PLC.

    The default rightOperandObjectType is D.

    \sa indent
*/
My::ObjectType MyPushButtonFourArithmetic::getRightOperandObjectType() const
{
    return d->rightOperandObjectType;
}

void MyPushButtonFourArithmetic::setRightOperandObjectType(My::ObjectType type)
{
    if(d->rightOperandObjectType == type)
        return;
    d->rightOperandObjectType = type;
}

/*!
    \property MyPushButtonFourArithmetic::leftOperandObjectIndex
    \brief the index of the leftOperandObject

    The object is coil or register of PLC.

    The default leftOperandObjectIndex is 0.

    \sa indent
*/
int MyPushButtonFourArithmetic::getLeftOperandObjectIndex() const
{
    return d->leftOperandObjectIndex;
}

void MyPushButtonFourArithmetic::setLeftOperandObjectIndex(int leftOpObIndex)
{
    if(d->leftOperandObjectIndex == leftOpObIndex)
        return;
    d->leftOperandObjectIndex = leftOpObIndex;
}

/*!
    \property MyPushButtonFourArithmetic::rightOperandObjectIndex
    \brief the index of the rightOperandObject

    The object is coil or register of PLC.

    The default rightOperandObjectIndex is 0.

    \sa indent
*/
int MyPushButtonFourArithmetic::getRightOperandObjectIndex() const
{
    return d->rightOperandObjectIndex;
}

void MyPushButtonFourArithmetic::setRightOperandObjectIndex(int rightOpObIndex)
{
    if(d->rightOperandObjectIndex == rightOpObIndex)
        return;
    d->rightOperandObjectIndex = rightOpObIndex;
}

/*!
    \property MyPushButtonFourArithmetic::arithmeticType
    \brief the type of the arithmetic

    The arithmetic is the funtion of MyPushButtonFourArithmetic.

    The default arithmetic is Addition.

    \sa indent
*/
My::FourArithmeticType MyPushButtonFourArithmetic::getArithmeticType() const
{
    return d->arithmeticType;
}

void MyPushButtonFourArithmetic::setArithmeticType(My::FourArithmeticType type)
{
    if(d->arithmeticType == type)
        return;
    d->arithmeticType = type;
}

/*!
    \property MyPushButtonFourArithmetic::leftOperandNumberType
    \brief the type of the leftOperandNumber

    The default leftOperandNumberType is Constant.

    \sa indent
*/
My::NumberType MyPushButtonFourArithmetic::getLeftOperandNumberType() const
{
    return d->leftOperandNumberType;
}

void MyPushButtonFourArithmetic::setLeftOperandNumberType(My::NumberType type)
{
    if(d->leftOperandNumberType == type)
        return;
    d->leftOperandNumberType = type;
}

/*!
    \property MyPushButtonFourArithmetic::rightOperandNumberType
    \brief the type of the rightOperandNumber

    The default rightOperandNumberType is Constant.

    \sa indent
*/
My::NumberType MyPushButtonFourArithmetic::getRightOperandNumberType() const
{
    return d->rightOperandNumberType;
}

void MyPushButtonFourArithmetic::setRightOperandNumberType(My::NumberType type)
{
    if(d->rightOperandNumberType == type)
        return;
    d->rightOperandNumberType = type;
}

/*!
    \property MyPushButtonFourArithmetic::leftOperandBaseType
    \brief the type of the leftOperandBase

    The default leftOperandBaseType is Dec.

    \sa indent
*/
My::NumberBaseType MyPushButtonFourArithmetic::getLeftOperandBaseType() const
{
    return d->leftOperandBaseType;
}

void MyPushButtonFourArithmetic::setLeftOperandBaseType(My::NumberBaseType type)
{
    if(d->leftOperandBaseType == type)
        return;
    d->leftOperandBaseType = type;
}

/*!
    \property MyPushButtonFourArithmetic::rightOperandBaseType
    \brief the type of the rightOperandBase

    The default rightOperandBaseType is Dec.

    \sa indent
*/
My::NumberBaseType MyPushButtonFourArithmetic::getRightOperandBaseType() const
{
    return d->rightOperandBaseType;
}

void MyPushButtonFourArithmetic::setRightOperandBaseType(My::NumberBaseType type)
{
    if(d->rightOperandBaseType == type)
        return;
    d->rightOperandBaseType = type;
}

/*!
    \property MyPushButtonFourArithmetic::leftOperand
    \brief the left constant type number of the arithmetic

    The arithmetic is the funtion of MyPushButtonFourArithmetic.

    The default leftOperand is 0.

    \sa indent
*/
double MyPushButtonFourArithmetic::getLeftOperand() const
{
    return d->leftOperand;
}

void MyPushButtonFourArithmetic::setLeftOperand(double leftOp)
{
    if(d->leftOperand == leftOp)
        return;
    d->leftOperand = leftOp;
}

/*!
    \property MyPushButtonFourArithmetic::rightOperand
    \brief the right constant type number of the arithmetic

    The arithmetic is the funtion of MyPushButtonFourArithmetic.

    The default rightOperand is 0.

    \sa indent
*/
double MyPushButtonFourArithmetic::getRightOperand() const
{
    return d->rightOperand;
}

void MyPushButtonFourArithmetic::setRightOperand(double leftOp)
{
    if(d->rightOperand == leftOp)
        return;
    d->rightOperand = leftOp;
}

/*!
    \property MyPushButtonFourArithmetic::resultDeviceIndex
    \brief the index of the resultDevice

    The default resultDeviceIndex is 0.

    \sa indent
*/
int MyPushButtonFourArithmetic::getResultDeviceIndex() const
{
    return d->resultDeviceIndex;
}

void MyPushButtonFourArithmetic::setResultDeviceIndex(int resultDevIndex)
{
    if(d->resultDeviceIndex == resultDevIndex)
        return;
    d->resultDeviceIndex = resultDevIndex;
}

/*!
    \property MyPushButtonFourArithmetic::resultObjectType
    \brief the type of the resultObject

    The object is coil or register of PLC.

    The default resultObjectType is D.

    \sa indent
*/
My::ObjectType MyPushButtonFourArithmetic::getResultObjectType() const
{
    return d->resultObjectType;
}

void MyPushButtonFourArithmetic::setResultObjectType(My::ObjectType type)
{
    if(d->resultObjectType == type)
        return;
    d->resultObjectType = type;
}

/*!
    \property MyPushButtonFourArithmetic::resultObjectIndex
    \brief the index of the resultObject

    The default resultObjectIndex is D.

    \sa indent
*/
int MyPushButtonFourArithmetic::getResultObjectIndex() const
{
    return d->resultObjectIndex;
}

void MyPushButtonFourArithmetic::setResultObjectIndex(int resultObIndex)
{
    if(d->resultObjectIndex == resultObIndex)
        return;
    d->resultObjectIndex = resultObIndex;
}

/*!
    \property MyPushButtonFourArithmetic::displayDefault
    \brief the model of the display

    The default displayDefault is true.

    \sa indent
*/
bool MyPushButtonFourArithmetic::getDisplayDefault() const
{
    return d->displayDefault;
}

void MyPushButtonFourArithmetic::setDisplayDefault(bool displayDefu)
{
    if(d->displayDefault == displayDefu)
        return;
    d->displayDefault = displayDefu;
}

/*!
    \reimp
*/
void MyPushButtonFourArithmetic::paintEvent(QPaintEvent *ev)
{
    //绘制准备工作,启用反锯齿
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

    if(d->displayDefault)
    {
        switch (d->arithmeticType)
        {
        case My::Addition:
            this->setText("Addition");
            break;
        case My::Subtraction:
            this->setText("Subtraction");
            break;
        case My::Multiplication:
            this->setText("Multiplication");
            break;
        case My::Division:
            this->setText("Division");
            break;
        default:
            return;
        }
    }

    QPushButton::paintEvent(ev);
}

/*!
 * \brief MyPushButtonFourArithmetic::enterEvent
 * \param ev
 */
void MyPushButtonFourArithmetic::enterEvent(QEvent *ev)
{
    if(QEvent::Enter == ev->type())
    {
        QCursor cursor;                           //创建光标对象
        cursor.setShape(Qt::PointingHandCursor);  //设置光标形状(指)
        setCursor(cursor);                        //使用光标

        update();
    }
}

/*!
 * \brief MyPushButtonFourArithmetic::leaveEvent
 * \param ev
 */
void MyPushButtonFourArithmetic::leaveEvent(QEvent *ev)
{
    if(QEvent::Leave == ev->type())
    {
        QCursor cursor;                           //创建光标对象
        cursor.setShape(Qt::ArrowCursor);         //设置光标形状(指)
        setCursor(cursor);                        //使用光标

        update();
    }
}

/*!
 * \brief MyPushButtonFourArithmetic::mousePressEvent
 * \param ev
 */
void MyPushButtonFourArithmetic::mousePressEvent(QMouseEvent *ev)
{
    if(Qt::LeftButton == ev->button())
    {
        d->LeftmousePressed = true;
        d->mousePressPoint = ev->globalPos();

        QCursor cursor;                         //创建光标对象
        cursor.setShape(Qt::ClosedHandCursor);  //设置光标形状(小手)
        setCursor(cursor);                      //使用光标

        update();
    }
}

/*!
 * \brief MyPushButtonFourArithmetic::mouseReleaseEvent
 * \param ev
 */
void MyPushButtonFourArithmetic::mouseReleaseEvent(QMouseEvent *ev)
{
    if(Qt::LeftButton == ev->button())
    {
        d->LeftmousePressed = false;

        QCursor cursor;                           //创建光标对象
        cursor.setShape(Qt::PointingHandCursor);  //设置光标形状(指)
        setCursor(cursor);                        //使用光标

        update();
    }

}

/*!
 * \brief MyPushButtonFourArithmetic::mouseMoveEvent
 * \param ev
 */
void MyPushButtonFourArithmetic::mouseMoveEvent(QMouseEvent *ev)
{
    // 鼠标左键拖动移动
    if(d->LeftmousePressed)
    {
        double mouseDx = ev->globalPos().x() - d->mousePressPoint.x();
        double mouseDy = ev->globalPos().y() - d->mousePressPoint.y();
        d->mousePressPoint = ev->globalPos();

        double myLabelNewx = this->x() + mouseDx;
        double myLabelNewy = this->y() + mouseDy;

        if(myLabelNewx < 0)
            myLabelNewx = 0;
        if(myLabelNewx + this->width() > QApplication::activeWindow()->width())
            myLabelNewx = QApplication::activeWindow()->width() - this->width();
        if(myLabelNewy < 0)
            myLabelNewy = 0;
        if(myLabelNewy + this->height() > QApplication::activeWindow()->height())
            myLabelNewy = QApplication::activeWindow()->height() - this->height();

        move(myLabelNewx, myLabelNewy);
        update();
    }
}


